package com.aaa.ssm.controller;

import com.aaa.ssm.entity.Emp;
import com.aaa.ssm.entity.Permission;
import com.aaa.ssm.mapper.PermissionMapper;
import com.aaa.ssm.service.EmpService;
import com.aaa.ssm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author AUSU
 * @version 1.0
 * 创建时间：2025/3/20 15:07
 */
@RestController
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private HttpSession session;

    @PostMapping
    public AjaxResult login(String username,String password){


        try{
            Emp login = empService.login(username, password);
            session.setAttribute("user",login);
            return AjaxResult.success(login);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    @GetMapping
    public AjaxResult getInfo(){

        try{
            Emp emp = (Emp) session.getAttribute("user");
            List<Permission> permission = empService.getPermission(emp.getRole().getId());
            HashMap<Object,Object> map = new HashMap<>();
            map.put("userInfo",emp);
            map.put("permissionInfo",permission);

            return AjaxResult.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }



}
